<?php

class Admin_ObjectLibPhotoController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->ADMINNAME;
    	$this->view->role = $infoUser->ROLE_ID;
    	$this->_model = new Default_Model_DbTable_ObjectLibPhoto();
    	$this->upload = new Private_Upload();
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Quản Lý Thư Viện Hình Ảnh","PREPEND");
        $this->view->title = "Quản Lý Thư Viện Hình Ảnh";
        $obj = new Default_Model_DbTable_Object();
        $this->view->allObj = $obj->getAllObject();
        $this->view->allLib = $this->_model->getAllObjectLibPhoto();
        if($this->_request->isPost()){
        	$oId = $this->_getParam("objectId");
        	$name = $this->_getParam("Name");
        	$desc = $this->_getParam("Desc");
        	$content = $this->_getParam("Content");
        	$mslide = $this->_getParam("MSlide");
        	$position = $this->_getParam("Position");
        	$stt = $this->_getParam("Stt");
        	if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
        		$img = "/homeviet/public/uploaded/images/noavatar.png";
        	}elseif ($this->_getParam("Link")){
        		$img = $this->_getParam("Link");
        	}else{
        		$img = "/homeviet/public/uploaded/images/libphoto/".$_FILES["Img"]["name"];
        		$this->upload->upload("libphoto");
        	}
        	$this->_model->addOLP($oId, $name, $img, $desc, $content, $mslide, $position, $stt);
        	$this->_redirect("admin/objectlibphoto");
        }
    }
    
    public function editAction(){
    	$this->view->headTitle("Sửa Thư Viện Ảnh","PREPEND");
    	$this->view->title = "Sửa Thư Viện Ảnh";
    	$id = $this->_getParam("id");
    	$libphoto = $this->_model->GetByID($id);#print_r($libphoto->toArray());die;
    	$this->view->libp = $libphoto;
    	$obj = new Default_Model_DbTable_Object();
    	$this->view->allObj = $obj->getAllObject();
    	if($this->_request->isPost()){
    		
    		$oId = $this->_getParam("objectId");
        	$name = $this->_getParam("Name");
        	$desc = $this->_getParam("Desc");
        	$content = $this->_getParam("Content");
        	$mslide = $this->_getParam("MSlide");
        	$position = $this->_getParam("Position");
        	$stt = $this->_getParam("Stt");
        	if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
        		$img = "/homeviet/public/uploaded/images/noavatar.png";
        	}elseif ($this->_getParam("Link")){
        		$img = $this->_getParam("Link");
        	}else{
        		$img = "/homeviet/public/uploaded/images/libphoto/".$_FILES["Img"]["name"];
        		$this->upload->upload("libphoto");
        	}
        	$this->_model->updateObjectLibPhoto($id,$oId, $name, $img, $desc, $content, $mslide, $position, $stt);
        	$this->_redirect("admin/objectlibphoto");
    	}
    }
    
    public function delAction(){
    	$id = $this->_getParam("id");
    	$libp = $this->_model->GetByID($id);
    	$patch = $libp["IMG"];
    	$olp = new Default_Model_DbTable_ObjectPhotoProfile();
    	$olp->removeByLibID($id);
    	if ($patch != "/homeviet/public/uploaded/images/noavatar.png"){
    		$this->upload->del_img(ROOT_DIR.$patch);
    	}
    	$this->_model->removeObjectLibPhoto($id);
    	$this->_redirect("admin/objectlibphoto");
    }
    
    public function activeAction(){
    	$id = $this->_getParam("id");
    	$this->_model->activeObjectLibPhoto($id);
    	$this->_redirect("admin/objectlibphoto");
    }


}

